package com.cherry.sdk.http.converter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

/**
 * 值类型的枚举解析处理
 *
 * @author 董棉生(dongmiansheng @ parkingwang.com)
 * @since 18-11-16
 */
public class ValueEnumAdapterFactory implements TypeAdapterFactory {
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        Class<?> rawType = type.getRawType();
        if (rawType.isEnum() && ValueEnum.class.isAssignableFrom(rawType)) {
            return (TypeAdapter<T>) new ValueEnumAdapter<>((Class<? extends Enum>) rawType);
        }
        return null;
    }
}
